package com.huatai.bpmn.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.huatai.bpmn.entity.TbBusinessEntity;
import com.huatai.bpmn.entity.dto.CandidateTaskDto;
import com.huatai.bpmn.entity.vo.CompleteTaskVO;
import com.huatai.bpmn.entity.vo.HistoryVO;
import com.huatai.bpmn.entity.vo.TaskVO;
import org.apache.ibatis.annotations.Select;

import java.util.List;


/**
* @author 聂贞吉
* @description 针对表【tb_business】的数据库操作Mapper
* @createDate 2023-07-07 15:34:25
* @Entity generator.domain.TbBusiness
*/

public interface TbBusinessMapper extends BaseMapper<TbBusinessEntity> {

	/**
	 * 查询用户已办的任务
	 * @param candidateTaskDto
	 * @return
	 */
	@Select("SELECT  " +
		"hit.PROC_INST_ID_ as procinstId, " +
		"hit.ID_ as taskId, " +
		"hit.FORM_KEY_ as nodeForm, " +
		"hit.NAME_ as nodeName, " +
		"bu.external_parameter_id as externalParameterId, " +
		"u.user_real_name as applicant, " +
		"c.approval_proposal as approvalProposal, " +
		"c.`status` as `status`, " +
		"(SELECT user_real_name FROM tb_user WHERE user_id=ASSIGNEE_)as assignee, " +
		"matter_name as matterName, " +
		"START_TIME_ as startTime, " +
		"END_TIME_ as endTime " +
		"FROM `act_hi_taskinst` hit " +
		"INNER JOIN tb_business bu " +
		"on hit.PROC_INST_ID_=bu.procinst_id " +
		"INNER JOIN tb_user u " +
		"on u.user_id=bu.create_userid " +
		"INNER JOIN tb_complete_task_status c " +
		"on c.task_id=hit.ID_ " +
		"where ASSIGNEE_=${userId}  ORDER BY bu.update_time  DESC limit ${page},${limit} ")
    List<CompleteTaskVO> findCompleteTask(CandidateTaskDto candidateTaskDto);


	@Select(
		"SELECT  " +
		"r.PROC_INST_ID_ as procinstId, " +
		"r.ID_ as taskId, " +
		"u.user_real_name as applicant, " +
		"\"等待处理\" as `status`, " +
		"b.matter_name as matterName, " +
		"b.create_time as createTime, " +
		"b.create_userid as userId, " +
		"b.deploy_id as deployId, " +
		"r.FORM_KEY_ as nodeForm, " +
		"IF(b.create_userid=${userId},'TRUE','FALSE') as isUpdate " +
		"FROM " +
		"tb_business b  " +
		"INNER JOIN act_ru_task r ON b.procinst_id = r.PROC_INST_ID_ " +
		"INNER JOIN act_ru_identitylink i on i.TASK_ID_ = r.ID_ " +
		"INNER JOIN tb_user u ON u.user_id = b.create_userid  " +
		"WHERE " +
		"(USER_ID_=${userId} or GROUP_ID_ in (${roleIds})) ORDER BY b.update_time DESC LIMIT ${page},${limit}  ")
	List<TaskVO> findToDoTask(CandidateTaskDto candidateTaskDto);

	@Select("SELECT  " +
		"\tb.matter_name as matterName, " +
		"\tIF(b.`status`='1','已完成','未完成') as status, " +
		"\tb.procinst_id as procinstId, " +
		"\tb.create_time as createTime, " +
		"\tu.user_real_name as applicant, " +
		"\tb.update_time AS endTime, " +
		"\t(SELECT user_real_name FROM tb_user where user_id=b.update_userid) as findAssignee, " +
		"\t(select FORM_KEY_ FROM act_hi_taskinst where PROC_INST_ID_=b.procinst_id ORDER BY END_TIME_ DESC limit 1) as nodeForm " +
		"FROM tb_business b " +
		"INNER JOIN tb_user u " +
		"ON b.create_userid=u.user_id " +
		"where `status`='1' AND b.deploy_key='${deployKey}' ORDER BY endTime DESC LIMIT ${page},${limit}")
	List<TaskVO> selectCompleteTaskAll(CandidateTaskDto candidateTaskDto);

	@Select("SELECT  " +
		"h.PROC_INST_ID_ as processInstanceId,h.EXECUTION_ID_, " +
		"h.TASK_ID_ as taskId, " +
		"h.ACT_NAME_ as activityName,h.ACT_TYPE_ as actType, " +
		"h.ASSIGNEE_, " +
		"h.START_TIME_ as startTime,h.END_TIME_ as endTime, " +
		"b.deploy_id as deployId, " +
		"t.FORM_KEY_ as nodeForm, " +
		"c.`status`,c.approval_proposal " +
		"FROM act_hi_actinst h " +
		"LEFT JOIN act_hi_taskinst t " +
		"ON h.TASK_ID_ = t.ID_ " +
		"LEFT JOIN tb_complete_task_status c " +
		"ON h.TASK_ID_=c.task_id " +
		"LEFT JOIN tb_business b " +
		"ON h.PROC_INST_ID_=b.procinst_id " +
		"WHERE h.PROC_INST_ID_='${procinstId}' " +
		"ORDER BY h.START_TIME_ ASC")
    List<HistoryVO> taskHistoryInfo(CandidateTaskDto candidateTaskDto);

	@Select("SELECT  " +
		"u.user_real_name as application, " +
		"IFNULL(r.FORM_KEY_,IFNULL( " +
		"(SELECT " +
		"t.FORM_KEY_ AS nodeForm  " +
		"FROM " +
		"act_hi_actinst h " +
		"LEFT JOIN act_hi_taskinst t ON h.TASK_ID_ = t.ID_ " +
		"LEFT JOIN tb_complete_task_status c ON h.TASK_ID_ = c.task_id  " +
		"WHERE " +
		"h.PROC_INST_ID_ = b.procinst_id " +
		"AND ACT_TYPE_ = 'userTask'  " +
		"ORDER BY " +
		"h.END_TIME_ DESC  " +
		"LIMIT 1), " +
		"'未绑定表单'  " +
		") ) as nodeForm, " +
		"b.create_time as createTime, " +
		"b.procinst_id as procinstId, " +
		"b.matter_name as matterName, " +
		"IF(b.`status`=1,\"已完成\",\"等待处理\") as status, " +
		"IFNULL(r.NAME_,'结束') as nodeName " +
		"FROM tb_business b " +
		"LEFT JOIN act_ru_task r " +
		"ON b.procinst_id=r.PROC_INST_ID_ " +
		"LEFT JOIN tb_user u " +
		"ON b.create_userid = u.user_id " +
		"WHERE create_userid='${userId}' ORDER BY b.update_time DESC LIMIT ${page},${limit} ")
	List<TaskVO> findOneselfApplication(CandidateTaskDto candidateTaskDto);

	@Select(
		" <script>" +
		" SELECT  " +
		" u.user_real_name as application, " +
		" IFNULL(r.FORM_KEY_, IFNULL((SELECT FORM_KEY_ FROM act_hi_taskinst aht WHERE b.procinst_id = aht.PROC_INST_ID_ ORDER BY END_TIME_ DESC limit 1),'未绑定表单')) as nodeForm, " +
		" b.create_time as createTime, " +
		" b.procinst_id as procinstId, " +
		" b.matter_name as matterName, " +
		" IF(b.`status`=1,\"已完成\",\"等待处理\") as status, " +
		" IFNULL(r.NAME_,'已结束') as nodeName, " +
		" b.deploy_id as deployId, " +
		" rd.NAME_ as deployName " +
		" <if test = 'attentionSql !=null and attentionSql != \"\"'> ,${attentionSql} </if>" +
		" FROM tb_business b " +
		" LEFT JOIN  act_ru_task r " +
		" ON b.procinst_id=r.PROC_INST_ID_  " +
		" LEFT JOIN tb_user u " +
		" ON b.create_userid = u.user_id " +
		" LEFT JOIN act_re_deployment rd " +
		" ON rd.ID_ = b.deploy_id " +
		" WHERE b.deploy_key = #{deployKey} " +
		" ORDER BY b.update_time DESC LIMIT ${page},${limit} " +
		" </script>")
	List<TaskVO> findOneselfApplicationAndDeployment(CandidateTaskDto candidateTaskDto);
}




